package com.zld.smarket.service.serviceImpl;

import com.zld.smarket.dao.StorehouseMapper;
import com.zld.smarket.pojo.GoodsType;
import com.zld.smarket.pojo.Storehouse;
import com.zld.smarket.pojo.Supplier;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.StorehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class StorehouseServiceImpl implements StorehouseService {
    @Autowired
    private StorehouseMapper storehouseMapper;
    @Override
    public ResponseData findList(Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<Storehouse> storehouseList = storehouseMapper.selectAllAndPage(page,limit);
        Long count = storehouseMapper.countStorehouse();
        return new ResponseData(storehouseList,count);
    }

    @Override
    public Integer deleteById(Long[] ids) {
        int j=0;
        try {
            //先判定数据是否存在
            for (Long id : ids) {
                Storehouse storehouse = storehouseMapper.selectByPrimaryKey(id);
                if(storehouse != null) {
                    j = storehouseMapper.deleteByPrimaryKey(id);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
//            设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return j;
    }

    @Override
    public ResponseData search(String s, Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<Storehouse> storehouseList =  storehouseMapper.search(s,page,limit);
        Long count = Long.valueOf(storehouseList.size());
        return new ResponseData(storehouseList,count);
    }

    @Override
    public Long selectbyno(Long storehouseNo) {
        return storehouseMapper.countStorehouseByNo(storehouseNo);
    }

    @Override
    public Integer addStorehouse(Storehouse storehouse) {
        int j=0;
        j=storehouseMapper.insert(storehouse);
        return j;
    }

    @Override
    public ResponseData updateById(Storehouse storehouse) {
        try {
            Storehouse storehouse1 = storehouseMapper.selectByPrimaryKey(storehouse.getStorehouseId());
            //还需要加逻辑判定  -- 去学习正则表达式
            if(storehouse1!=null){
                int i =  storehouseMapper.updateByPrimaryKey(storehouse);
                if(i==0){
                    return new ResponseData(ExceptionMsg.FAILD);
                }else{
                    return new ResponseData(ExceptionMsg.SUCCESS);
                }
            }else {
                return new ResponseData("000407","请刷新以后在尝试！");
            }

        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ExceptionMsg.FAILD);
        }
    }

    @Override
    public List<Storehouse> selectAll() {
        return storehouseMapper.selectAll();
    }

}
